package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.ItemDesignFile;
import com.mxpio.erp.common.technology.entity.ItemDesignFileAlter;
import com.mxpio.erp.common.technology.entity.ItemDesignFileSignIn;
import com.mxpio.erp.common.technology.service.ItemDesignFileService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ItemDesignFileServiceImpl extends BaseServiceImpl<ItemDesignFile> implements ItemDesignFileService {

    @Transactional
    @Override
    public ItemDesignFile save(ItemDesignFile entity){
        if(StringUtils.isEmpty(entity.getFamily())){
            entity.setFamily(UUID.randomUUID().toString().replace("-",""));
        }
        super.save(entity);
        return entity;
    }

    @Override
    @Transactional
    public Result<ItemDesignFile> signIn(ItemDesignFile itemDesignFile) {
        String id = itemDesignFile.getId();
        ItemDesignFile file = this.getById(ItemDesignFile.class, id);
        if (file==null){
            return Result.error("入参中的id无效，未能匹配当前版次数据");
        }
        if (!file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
            return Result.error("仅签出操作状态下允许签入操作");
        }
        if (!file.getSignOffer().equals(SecurityUtils.getLoginUsername())){
            return Result.error("仅签出人本人可以签入操作");
        }
        Long count = JpaUtil.linq(ItemDesignFileAlter.class).equal("family", file.getFamily()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()).count();
        if (count>0){
            return Result.error("已经有审核中的变更申请，无法进行签入操作");
        }
        //插入新版次数据
        ItemDesignFile addFile = new ItemDesignFile();
        addFile.setFamily(file.getFamily());
        addFile.setVersion(file.getVersion());
        addFile.setItemCode(file.getItemCode());
        addFile.setEdition(file.getEdition()+1);
        addFile.setFileNo(itemDesignFile.getFileNo());
        addFile.setFileName(itemDesignFile.getFileName());
        addFile.setLength(itemDesignFile.getLength());
        addFile.setExtension(itemDesignFile.getExtension());
        addFile.setRemarks(itemDesignFile.getRemarks());
        addFile.setSignIner(SecurityUtils.getLoginUsername());
        addFile.setSignInTime(new Date());
        addFile.setSignInDesc(itemDesignFile.getSignInDesc());
        save(addFile);

        //更新原版次
        file.setSignOffFlag(CommonEnums.YesNo.NO.getCode());
        file.setPrimary(CommonEnums.YesNo.NO.getCode());
        update(file);
        return Result.OK(addFile);
    }

    @Override
    @Transactional
    public Result<ItemDesignFileSignIn> signInApply(ItemDesignFileSignIn itemDesignFileSignIn) {
        String designFileId = itemDesignFileSignIn.getDesignFileId();
        ItemDesignFile file = this.getById(ItemDesignFile.class, designFileId);
        if (file==null){
            return Result.error("入参中的designFileId无效，未能匹配当前版本数据");
        }
        if (!file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
            return Result.error("仅签出操作状态下允许签入操作");
        }
        if (!file.getSignOffer().equals(SecurityUtils.getLoginUsername())){
            return Result.error("仅签出人本人可以签入操作");
        }
        Long count = JpaUtil.linq(ItemDesignFileSignIn.class).equal("family", file.getFamily()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()).count();
        if (count>0){
            return Result.error("已经有审核中的签入申请，请勿重复提交申请");
        }
        ItemDesignFileSignIn signIn = new ItemDesignFileSignIn();
        signIn.setDesignFileId(designFileId);
        signIn.setSignInDesc(itemDesignFileSignIn.getSignInDesc());
        signIn.setFileNo(itemDesignFileSignIn.getFileNo());
        signIn.setFileName(itemDesignFileSignIn.getFileName());
        signIn.setLength(itemDesignFileSignIn.getLength());
        signIn.setRemarks(itemDesignFileSignIn.getRemarks());
        signIn.setExtension(itemDesignFileSignIn.getExtension());
        signIn.setItemCode(itemDesignFileSignIn.getItemCode());
        signIn.setFamily(file.getFamily());
        JpaUtil.save(signIn);

        return Result.OK("签入申请提交成功",signIn);
    }

    @Override
    @Transactional
    public Result<ItemDesignFileAlter> alter(ItemDesignFileAlter itemDesignFileAlter) {
        String designFileId = itemDesignFileAlter.getDesignFileId();
        ItemDesignFile file = this.getById(ItemDesignFile.class, designFileId);
        if (file==null){
            return Result.error("入参中的designFileId无效，未能匹配当前版本数据");
        }
        if (!file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
            return Result.error("仅签出操作状态下允许变更操作");
        }
        if (!file.getSignOffer().equals(SecurityUtils.getLoginUsername())){
            return Result.error("仅签出人本人可以变更操作");
        }
        Long count = JpaUtil.linq(ItemDesignFileAlter.class).equal("family", file.getFamily()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()).count();
        if (count>0){
            return Result.error("已经有审核中的变更申请，请勿重复操作");
        }
        ItemDesignFileAlter alter = new ItemDesignFileAlter();
        alter.setDesignFileId(designFileId);
        alter.setItemCode(file.getItemCode());
        alter.setAlterContent(itemDesignFileAlter.getAlterContent());
        alter.setFileNo(itemDesignFileAlter.getFileNo());
        alter.setFileName(itemDesignFileAlter.getFileName());
        alter.setLength(itemDesignFileAlter.getLength());
        alter.setRemarks(itemDesignFileAlter.getRemarks());
        alter.setExtension(itemDesignFileAlter.getExtension());
        alter.setFamily(file.getFamily());
        JpaUtil.save(alter);

        return Result.OK("变更申请提交成功",alter);
    }

    @Override
    @Transactional
    public List<ItemDesignFile> list(Criteria criteria) {
        List<ItemDesignFile> fileList = JpaUtil.linq(ItemDesignFile.class).where(criteria).list();
        if (!fileList.isEmpty()){
            for (ItemDesignFile file:fileList){//关联对应的变更、签入记录
                ItemDesignFileAlter alter = JpaUtil.linq(ItemDesignFileAlter.class).equal("alterCode", file.getChangeCode()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode()).findOne();
                if (alter!=null){
                    file.setAlter(alter);
                }

                ItemDesignFileSignIn signIn = JpaUtil.linq(ItemDesignFileSignIn.class).equal("code", file.getSignInCode()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode()).findOne();
                if (signIn!=null){
                    file.setSignIn(signIn);
                }
            }
        }
        return fileList;
    }

    @Override
    @Transactional
    public Result<ItemDesignFile> signOutCancel(String id) {
        ItemDesignFile file = this.getById(ItemDesignFile.class, id);
        if (file==null){
            return Result.error("未匹配到数据");
        }
        if (!file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
            return Result.error("仅签出操作状态下允许取消签出操作");
        }
        if (!file.getSignOffer().equals(SecurityUtils.getLoginUsername())){
            return Result.error("仅签出人自己可以取消签出操作");
        }
        Long count = JpaUtil.linq(ItemDesignFileSignIn.class).equal("family", file.getFamily()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()).count();
        if (count>0){
            return Result.error("已经有审核中的签入申请，无法取消签出操作");
        }
        file.setSignOffFlag(CommonEnums.YesNo.NO.getCode());
        file.setSignOffer(null);
        file.setSignOffTime(null);
        this.update(file);
        return Result.OK(file);
    }
}
